package com.haima.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.haima.common.utils.StringUtils;
import com.haima.system.service.ISysUserService;
import com.haima.utils.GenStr;
import com.haima.utils.WxTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.haima.common.constant.Constants;
import com.haima.common.core.domain.AjaxResult;
import com.haima.common.core.domain.entity.SysMenu;
import com.haima.common.core.domain.entity.SysUser;
import com.haima.common.core.domain.model.LoginBody;
import com.haima.common.utils.SecurityUtils;
import com.haima.framework.web.service.SysLoginService;
import com.haima.framework.web.service.SysPermissionService;
import com.haima.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author hima
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysUserService sysUserService;

    private String def_password = "ZilIao+20241019";

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    @PostMapping({"/loginForWechat"})
    public AjaxResult loginForWechat(@RequestBody LoginBody loginBody) {

        String openid = WxTool.getLoginInfo(loginBody.getCode());
        if (StringUtils.isEmpty(openid)) {
            return AjaxResult.error("微信登录失败");
        }

        Map<String, Object> map = new HashMap();
        String token = "";

        SysUser nowsu = this.sysUserService.selectUserByUserName(openid);
        if (nowsu == null) {
            SysUser reguser = new SysUser();
            reguser.setUserName(openid);
            reguser.setPassword(SecurityUtils.encryptPassword(this.def_password));
            reguser.setPhonenumber("");
            reguser.setNickName("小蜜蜂"+ GenStr.getRandomString(5));
            boolean regFlag = this.sysUserService.registerUser(reguser);
            if (!regFlag) {
                return AjaxResult.error("微信登录失败");
            }
            token = this.loginService.login(openid, this.def_password, loginBody.getCode(), loginBody.getUuid());
            nowsu = this.sysUserService.selectUserByUserName(openid);
        } else {
            token = this.loginService.login(openid, this.def_password, loginBody.getCode(), loginBody.getUuid());
        }

        map.put("token", token);
        map.put("userInfo", nowsu);
        map.put("islogin", true);
        return AjaxResult.success(map);
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
